// packagBuy/pages/found/component/commentItem/index.js
import { foundApi } from '../../../../../config/config'
const getURL = require('../../../../../utils/util')   //引入配置接口文件
const app=getApp();
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    itemData: {
      type: Object,
      value:{},
      observer(newVal, oldVal, changedPath) {
        // this._propertyChange(newVal)
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 删除评论
    deleteComment(e){
      const id=e.currentTarget.dataset.id
      wx.showModal({
        title: '确认删除',
        content: '确定要删除该评论吗？',
        cancelColor:'#888888',
        confirmColor:'#16BC83',
        success: (res)=> {
          if (res.confirm) {
            this.confirmDeleteComment(id)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    //  确定删除评论
    confirmDeleteComment(id){
      app.reqFun.requestForm('DELETE',foundApi.commentDel, {id}, (response) => {
        if(response.data){
          const res=response.data
          if (res.success == 1) {
            // 删除成功后移除评论数据
            let commentList=this.data.commentList
            const index=commentList.findIndex(v=>v.id==id)
            if(index>-1){
              commentList.splice(index,1)
              this.setData({
                commentList
              })
            }
          }else{
            wx.showToast({
              title: (res.data && res.data.msg) || '网络异常',
              icon:'none'
            })
          }
        }
      })
    },
    // 评论点赞/取消点赞
    thumbsUpComment(e){
      const id=e.currentTarget.dataset.id
      let commentList=this.data.commentList
      const index=commentList.findIndex(v=>v.id==id)
      const currentComment=commentList[index]
      const isThumbsUp=currentComment.isThumbsUp // 是否已点赞
      if(isThumbsUp){
        // 取消点赞
        const apiUrl=`${foundApi.removeThumbsUp}${currentComment.thumbsUpId}`
        app.reqFun.requestJson('DELETE',apiUrl, {}, (response) => {
          if(response.data){
            const res=response.data
            if(res.success===1){
              currentComment.isThumbsUp=false
              currentComment.thumbsUpNumber=currentComment.thumbsUpNumber-1
              currentComment.thumbsUpId=''
              commentList[index]=currentComment
              this.setData({
                commentList
              })
            }else{
              wx.showToast({
                title: (res.data && res.data.msg) || '网络异常',
                icon:'none'
              })
            }
          }
        })
      }else{
        // 点赞
        const params={
          id,
          type:'comment', // 点赞类型
        }
        app.reqFun.requestJson('GET',foundApi.thumbsUp, params, (response) => {
          if(response.data){
            const res=response.data
            if(res.data && res.success===1){
              currentComment.isThumbsUp=true
              currentComment.thumbsUpNumber=currentComment.thumbsUpNumber+1
              currentComment.thumbsUpId=res.data
              commentList[index]=currentComment
              this.setData({
                commentList
              })
            }else{
              wx.showToast({
                title: (res.data && res.data.msg) || '网络异常',
                icon:'none'
              })
            }
          }
        })
      }
    },
  }
})
